package com.portal4kids.controller;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.portal4kids.model.People;
import com.portal4kids.model.PeopleAttributes;
import com.portal4kids.model.PeopleVisitPlan;
import com.portal4kids.model.Places;
import com.portal4kids.util.HibernateUtil;

public class PeopleManager extends HibernateUtil{
	
	public People savePeople(People people, PeopleAttributes attributes){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		people.setAvarage(0);
		people.setNumberOfVotes(0);
		people.setRating(0);
		people.setComments("");
		
		System.out.println("Atr: " + attributes);
		List<PeopleAttributes> attribute = new ArrayList<PeopleAttributes>();
		attributes.setPeople(people);
		attribute.add(attributes);
		people.setAttributes(attribute);
		
		List<PeopleVisitPlan> visitPlan = new ArrayList<PeopleVisitPlan>();
		PeopleVisitPlan plan = new PeopleVisitPlan();
		plan.setPeople(people);
		visitPlan.add(plan);
		people.setVisitPlan(visitPlan);
		
		session.save(people);
		session.getTransaction().commit();
		return people;
		
	}
	
	public List<People> getPeopleAfterType(People people, PeopleAttributes attributes){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<People> listOfPeople = new ArrayList<People>();
		List<People> finalList = new ArrayList<People>();
		if("Dowolna".equals(people.getAdress())){
			Query q = session.createQuery("from People where type='"+ people.getType() + "' ORDER BY(avarage) DESC");
			listOfPeople = q.list();
			finalList = getPeopleAfterAttributes(people, attributes, listOfPeople);
		}else{
			Query q = session.createQuery("from People where type='"+ people.getType() + "' and adress='" + people.getAdress() + "' ORDER BY(avarage) DESC");
			listOfPeople = q.list();
			finalList = getPeopleAfterAttributes(people, attributes, listOfPeople);
		}
		
		session.getTransaction().commit();
		return finalList;
		
	}
	
	public List<People> getPeopleAfterAttributes(People people, PeopleAttributes attributes, List<People> listOfPeople ){
		List<People> finalList = new ArrayList<People>();
		if(("Lekarze".equals(people.getType()) || "Opiekunki do dzieci".equals(people.getType())) && (!listOfPeople.isEmpty()) ){
			List<PeopleAttributes> attributesFromUser = new ArrayList<PeopleAttributes>();
			attributesFromUser.add(attributes);
			List<PeopleAttributes> attributesFromDB = new ArrayList<PeopleAttributes>();	
			for (People peoples : listOfPeople) {
				attributesFromDB = peoples.getAttributes();
				if(attributesFromDB.iterator().next().getAttributeAfterType(people.getType()).containsAll(attributesFromUser.iterator().next().getAttributeAfterType(people.getType()))){
					finalList.add(peoples);
				}else if(("Lekarze".equals(people.getType()) && (attributesFromUser.iterator().next().getD_A1().equals("NIE")))){
					if(attributesFromUser.iterator().next().getSpecialization().equals(attributesFromDB.iterator().next().getSpecialization())){
						finalList.add(peoples);
					}
				}
			}
			
		}
		return finalList;
	}
	
	public List<People> getSelectedPeople(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<People> listOfPeople = new ArrayList<People>();
		Query q = session.createQuery("from People where id_people="+ id);
		listOfPeople = q.list();
		session.getTransaction().commit();
		return listOfPeople;
	}
	
	public static void addRank(int id_place, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		People people = (People) session.load(People.class, id_place);
		
//		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
//		symbols.setDecimalSeparator('.');
//		DecimalFormat df = new DecimalFormat("#.##", symbols);
		
		int numberOfVotes = people.getNumberOfVotes();
		numberOfVotes++;
		people.setNumberOfVotes(numberOfVotes);
		
		int currentRank = people.getRating();
		currentRank += rank;
		people.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		people.setAvarage(avarage);
		}
		session.update(people);
		session.getTransaction().commit();

	}
	
	public People getPeopleIfRegister(String login, String password){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<People> listOfPeople = new ArrayList<People>();
		Query q = session.createQuery("from People where login='"+ login + "' and password='" + password + "'");
		listOfPeople = q.list();
		
		People people = null;
		if(!listOfPeople.isEmpty()){
			for (People peoples : listOfPeople) {
				people = (People) session.load(People.class, peoples.getId_people());
				System.out.println(people.getVisitPlan());
			}
			
		}
		session.getTransaction().commit();
		return people;
	}
	
	public PeopleVisitPlan editVisitPlan(PeopleVisitPlan plan, int id_people){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		PeopleVisitPlan visitPlan = (PeopleVisitPlan) session.load(PeopleVisitPlan.class, plan.getId_plan());
		People people = (People) session.load(People.class, id_people);
		visitPlan = plan;
		visitPlan.setPeople(people);
		session.update(visitPlan);
		session.getTransaction().commit();
		return plan;
	}
	
	public List<PeopleVisitPlan> getPlanListFromDB(int id_people){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<PeopleVisitPlan> planList = new ArrayList<PeopleVisitPlan>();
		Query q = session.createQuery("from PeopleVisitPlan where people=" + id_people);
		planList = q.list();
		session.getTransaction().commit();
		return planList;
	}

}
